package com.android.gufengmall.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.gufengmall.App;
import com.android.gufengmall.R;
import com.android.gufengmall.adapter.ApparelAdapter;
import com.android.gufengmall.bean.ApparelBean;
import com.android.gufengmall.gen.ApparelBeanDao;
import com.android.gufengmall.gen.DaoSession;

import java.util.ArrayList;
import java.util.List;

/**
 * 服饰知识专区
 */
public class ApparelFragment extends Fragment implements ApparelAdapter.CommentListener {

    private RecyclerView mRvApparel;

    private ArrayList<ApparelBean> mApparelBeans;
    private ApparelAdapter mApparelAdapter;
    private App mMyApp;
    private DaoSession mDaoSession;
    private ApparelBeanDao mApparelBeanDao;

    public ApparelFragment() {
    }

    public static ApparelFragment newInstance() {
        return new ApparelFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_apparel, container, false);
        initView(view);
        initData();
        return view;
    }

    private void initView(View view) {
        mRvApparel = view.findViewById(R.id.rv_apparel);
    }

    private void initData() {
        mApparelBeans = new ArrayList<>();

        mMyApp = (App) getActivity().getApplication();
        mDaoSession = mMyApp.getDaoSession();
        mApparelBeanDao = mDaoSession.getApparelBeanDao();
        List<ApparelBean> apparelBeans = mApparelBeanDao.loadAll();
        mApparelBeans.addAll(apparelBeans);

        LinearLayoutManager layoutManager = new LinearLayoutManager(requireContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRvApparel.setLayoutManager(layoutManager);

        mApparelAdapter = new ApparelAdapter(requireContext(), mApparelBeans);
        mApparelAdapter.setOnCommentListener(this);
        mRvApparel.setAdapter(mApparelAdapter);

    }

    @Override
    public void onLikeListener(int position) {
        ApparelBean apparelBean = mApparelBeanDao.loadByRowId(position);
        boolean isLike = apparelBean.getIsLike();
        apparelBean.setIsLike(!isLike);
        mApparelBeanDao.update(apparelBean);
        if (isLike) {
            Toast.makeText(requireContext(), "取消点赞", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(requireContext(), "点赞成功", Toast.LENGTH_SHORT).show();
        }
        mApparelAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCollectListener(int position) {
        ApparelBean apparelBean = mApparelBeanDao.loadByRowId(position);
        boolean isCollect = apparelBean.getIsCollect();
        apparelBean.setIsCollect(!isCollect);
        mApparelBeanDao.update(apparelBean);
        if (isCollect) {
            Toast.makeText(requireContext(), "取消收藏", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(requireContext(), "收藏成功", Toast.LENGTH_SHORT).show();
        }
        mApparelAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemSelectListener(String position) {
        Intent intent = new Intent(requireContext(), CommentActivity.class);
        intent.putExtra("status", 1);
        intent.putExtra("position", position);
        startActivity(intent);
    }
}